import 'package:flutter/material.dart';
import 'package:flutter_getx/app/const.dart';
import 'package:flutter_getx/app/routes/app_pages.dart';
import 'package:flutter_getx/app/util/date.dart' as util;

import 'package:get/get.dart';

import '../controllers/msglist_controller.dart';

class MsgListView extends GetView<MsgListController> {
  @override
  Widget build(BuildContext context) {
    final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        leading: GestureDetector(
            onTap: () {
              _scaffoldKey.currentState?.openDrawer();
            },
            child: Icon(Icons.menu)),
        title: Text('消息'),
        centerTitle: true,
        actions: [
          GestureDetector(
            onTap: () {
              controller.refreshChatList();
            },
            child: Icon(
              Icons.refresh,
              color: Colors.white,
              size: 34,
            ),
          ),
          PopupMenuButton(
            icon: Icon(
              Icons.add_circle_outline,
              color: Colors.white,
              size: 34,
            ),
            itemBuilder: (BuildContext context) =>
                <PopupMenuEntry<SelectItemAction>>[
              PopupMenuItem<SelectItemAction>(
                value: SelectItemAction.CreateGroup,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Icon(Icons.maps_ugc, color: Colors.black),
                    Text("创建群聊"),
                  ],
                ),
              ),
              PopupMenuItem<SelectItemAction>(
                value: SelectItemAction.AddGroupOrFriend,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Icon(Icons.person_add_alt_1_rounded, color: Colors.black),
                    Text("加好友/群"),
                  ],
                ),
              ),
            ],
            onSelected: (SelectItemAction item) async {
              await controller.onMenuSelected(item);
            },
          )
        ],
      ),
      body: Container(
        color: bgColor,
        child: Obx(() => ListView.builder(
              itemCount: controller.messageService.chatList.length,
              itemBuilder: (context, index) {
                return Container(
                  color: Colors.white,
                  child: Column(
                    children: [
                      ListTile(
                        leading: controller.messageService.chatList[index]['url'] == null || controller.messageService.chatList[index]['url'] == ""? Icon(Icons.account_circle_rounded, size: 60,):CircleAvatar(
                          radius: 34,
                          backgroundColor: Colors.white,
                          backgroundImage: NetworkImage(
                              controller.messageService.chatList[index]['url']),
                        ),
                        title: Text(
                          controller.messageService.chatList[index]['user'],
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                        // subtitle: Text(controller.messageService.chatList[index]['sub'] == null
                        //     ? ""
                        //     : controller.messageService.chatList[index]['sub']),
                        trailing: controller.messageService.undRead.contains(
                                controller.messageService.chatList[index]
                                    ['fromUserId'])
                            ? Icon(
                                Icons.circle,
                                color: Colors.greenAccent,
                                size: 10,
                              )
                            : null,
                        onTap: () {
                          controller.toChat(index);
                        },
                      ),
                      Divider(
                        height: 1,
                        indent: 10,
                      ),
                    ],
                  ),
                  padding: EdgeInsets.only(bottom: 5, top: 5),
                );
              },
            )),
      ),
      drawer: Drawer(
        child: Obx(() => ListView(
              children: [
                if (controller.userInfoService.userInfo.value.id == null)
                  Container()
                else
                  UserAccountsDrawerHeader(
                    accountName: Text(
                        controller.userInfoService.userInfo.value.nickname!),
                    currentAccountPicture:
                        controller.userInfoService.userInfo.value.avatar == ""
                            ? Icon(
                                Icons.account_circle_rounded,
                                size: 60,
                          color: Colors.white38,
                              )
                            : CircleAvatar(
                                backgroundImage: NetworkImage(controller
                                    .userInfoService.userInfo.value.avatar!),
                              ),
                    accountEmail: Text("账号: " +
                        controller.userInfoService.userInfo.value.id!
                            .toString()),
                  ),
                if (controller.userInfoService.userInfo.value.id == null)
                  ListTile(
                      leading: Icon(
                        Icons.people,
                        color: Colors.blue,
                      ),
                      title: new Text('登录'),
                      trailing: new Icon(Icons.arrow_right),
                      onTap: () {
                        Navigator.of(context).pop();
                        Get.toNamed(Routes.LOGIN);
                      })
                else
                  ListTile(
                      leading: Icon(
                        Icons.logout,
                        color: Colors.blue,
                      ),
                      title: new Text('退出登录'),
                      trailing: new Icon(Icons.arrow_right),
                      onTap: () {
                        Navigator.of(context).pop();
                        Get.defaultDialog(
                            title: "提示",
                            middleText: "您确定退出登录？",
                            confirm: ElevatedButton(
                                onPressed: () {
                                  controller.logout();
                                },
                                child: Text("确定")),
                            cancel: ElevatedButton(
                                onPressed: () {
                                  Get.back();
                                },
                                child: Text("取消")));
                      }),
              ],
            )),
      ),
    );
  }
}
